package com.woniuxy01;

import java.io.*;
import java.util.Arrays;

/**
 * @author 作者
 * @create 2025-06-10 14:49
 */
public class Test05 {

    public static void main(String[] args) throws IOException {
        //字符流读写二进制文件
        FileReader fr=new FileReader("d:\\aaa\\a.mp4");
        FileWriter fw=new FileWriter("d:\\aaa\\b.mp4");
        char[] chars=new char[1024];
        int len;
        while((len=fr.read(chars))!=-1){
            fw.write(chars,0,len);
        }
        fr.close();
        fw.close();
    }

    private static void test05() throws IOException {
        FileWriter fw=new FileWriter("b.txt");
        fw.write("aaaa中文");
        fw.flush();
        fw.close();

        FileReader fr=new FileReader("b.txt");
        int mychar;
        while((mychar=fr.read())!=-1){
            System.out.println((char)mychar);
        }
        fr.close();
    }

    private static void test04() throws IOException {
        //字符流，一次读一个字符数组
        InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");
        int len;
        char[] chars=new char[3];
        while((len=isr.read(chars))!=-1){
            System.out.println(new String(chars,0,len));
        }
        isr.close();
    }

    private static void test03() throws IOException {
        //字符流，一次读一个字符
        InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
        int mychar = 0;
        while ((mychar = isr.read()) != -1) {
            System.out.print((char) mychar);
        }
        isr.close();
    }

    private static void test02() throws IOException {
        //字符流=字节流+编码表
        //一次读写一个字符或者一个字符串
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"),"gbk");
        osw.write("abc");
        osw.write('中');
        osw.write("文字符");
        //字符流，读写一段数据以后，调用flush
        osw.flush();
        osw.close();
    }

    private static void test01() throws UnsupportedEncodingException {
        String str="中文";
        byte[] bytes1 = str.getBytes("utf-8");
        System.out.println(Arrays.toString(bytes1));
        System.out.println(new String(bytes1, "iso-8859-1"));
    }
}
